extends VBoxContainer

@onready var master_volume_toggle := %MasterEnabledToggle
@onready var master_volume_slider := %MasterVolumeSlider
@onready var music_volume_toggle := %MusicEnabledToggle
@onready var music_volume_slider := %MusicVolumeSlider
@onready var sound_volume_toggle := %SoundEnabledToggle
@onready var sound_volume_slider := %SoundVolumeSlider
@onready var language_dropdown := %LanguageDropdown
@onready var resolution_dropdown: OptionButton = %ResolutionDropdown

# 快捷键按钮引用

@onready var attack_button := %AttackButton
@onready var interaction_button := %InteractionButton
@onready var w_button := %WButton
@onready var s_button := %SButton
@onready var a_button := %AButton
@onready var d_button := %DButton
@onready var skill1_button := %"1Button"
@onready var skill2_button := %"2Button"
@onready var skill3_button := %"3Button"
@onready var skill4_button := %"4Button"
@onready var skill5_button := %"5Button"
@onready var dash_button := %"6Button"

# 快捷键绑定相关变量
var current_listening_button: Button = null
var current_listening_action: String = ""
var is_listening = false

## maps the index of a locale to the locale itself
var locales:PackedStringArray = []
var resolutions = [
	Vector2i(1920, 1080),
	Vector2i(1280, 720)
]


func _ready() -> void:
	# 初始化快捷键绑定管理器
	UserSettings.key_binding_changed.connect(_on_key_binding_changed)
	_setup_keybindings()
	# 设置语言
	self.locales = TranslationServer.get_loaded_locales()
	var current_locale = TranslationServer.get_locale()
	var idx = 0
	var select_index = -1
	for locale in locales:
		var language = TranslationServer.get_locale_name(locale)
		language_dropdown.add_item(language, idx)
		if current_locale == locale:
			select_index = idx
		idx += 1
	language_dropdown.select(select_index)		
	
	# 设置分辨率
	idx = 0
	current_locale = UserSettings.get_value(UserSettings.RESOLUTION)
	select_index = -1
	for resolution in resolutions:
		resolution_dropdown.add_item(str(resolution), idx)
		if current_locale == resolution:
			select_index = idx
		idx += 1
	resolution_dropdown.select(select_index)
		
func _on_master_volume_toggle_toggled(button_pressed: bool) -> void:
	master_volume_slider.editable = button_pressed
	music_volume_slider.editable = music_volume_toggle.button_pressed and button_pressed
	sound_volume_slider.editable = sound_volume_toggle.button_pressed and button_pressed
	UserSettings.set_value("mastervolume_enabled", button_pressed)


func _on_music_enabled_toggle_toggled(button_pressed: bool) -> void:
	music_volume_slider.editable = master_volume_toggle.button_pressed and button_pressed
	UserSettings.set_value("musicvolume_enabled", button_pressed)


func _on_sound_enabled_toggle_toggled(button_pressed: bool) -> void:
	sound_volume_slider.editable = master_volume_toggle.button_pressed and button_pressed
	UserSettings.set_value("soundvolume_enabled", button_pressed)


func _on_language_dropdown_item_selected(index: int) -> void:
	UserSettings.set_value("game_locale", locales[index])


func _on_resolution_dropdown_item_selected(index: int) -> void:
	UserSettings.set_value("resolution", resolutions[index])

# 快捷键绑定相关函数
func _setup_keybindings():
	# 连接所有快捷键按钮的信号
	_connect_keybinding_buttons()
	# 更新按钮显示
	_update_keybinding_buttons()

func _connect_keybinding_buttons():
	# 连接攻击键按钮
	if attack_button:
		attack_button.pressed.connect(_on_attack_button_pressed)
	
	# 连接交互键按钮
	if interaction_button:
		interaction_button.pressed.connect(_on_interaction_button_pressed)
	
	# 连接移动键按钮
	if w_button:
		w_button.pressed.connect(_on_w_button_pressed)
	if s_button:
		s_button.pressed.connect(_on_s_button_pressed)
	if a_button:
		a_button.pressed.connect(_on_a_button_pressed)
	if d_button:
		d_button.pressed.connect(_on_d_button_pressed)
	
	# 连接技能键按钮
	if skill1_button:
		skill1_button.pressed.connect(_on_skill1_button_pressed)
	if skill2_button:
		skill2_button.pressed.connect(_on_skill2_button_pressed)
	if skill3_button:
		skill3_button.pressed.connect(_on_skill3_button_pressed)
	if skill4_button:
		skill4_button.pressed.connect(_on_skill4_button_pressed)
	if skill5_button:
		skill5_button.pressed.connect(_on_skill5_button_pressed)
	
	# 连接冲刺键按钮
	if dash_button:
		dash_button.pressed.connect(_on_dash_button_pressed)

func _update_keybinding_buttons():
	if not UserSettings:
		return
	
	# 更新所有按钮的显示文本
	if attack_button:
		attack_button.text = UserSettings.get_key_display_text("shoot")
	if interaction_button:
		interaction_button.text = UserSettings.get_key_display_text("interact")
	if w_button:
		w_button.text = UserSettings.get_key_display_text("up")
	if s_button:
		s_button.text = UserSettings.get_key_display_text("down")
	if a_button:
		a_button.text = UserSettings.get_key_display_text("left")
	if d_button:
		d_button.text = UserSettings.get_key_display_text("right")
	if skill1_button:
		skill1_button.text = UserSettings.get_key_display_text("skill1")
	if skill2_button:
		skill2_button.text = UserSettings.get_key_display_text("skill2")
	if skill3_button:
		skill3_button.text = UserSettings.get_key_display_text("skill3")
	if skill4_button:
		skill4_button.text = UserSettings.get_key_display_text("skill4")
	if skill5_button:
		skill5_button.text = UserSettings.get_key_display_text("skill5")
	if dash_button:
		dash_button.text = UserSettings.get_key_display_text("dash")

func _on_attack_button_pressed():
	_start_listening("shoot", attack_button)

func _on_interaction_button_pressed():
	_start_listening("interact", interaction_button)

func _on_w_button_pressed():
	_start_listening("up", w_button)

func _on_s_button_pressed():
	_start_listening("down", s_button)

func _on_a_button_pressed():
	_start_listening("left", a_button)

func _on_d_button_pressed():
	_start_listening("right", d_button)

func _on_skill1_button_pressed():
	_start_listening("skill1", skill1_button)

func _on_skill2_button_pressed():
	_start_listening("skill2", skill2_button)

func _on_skill3_button_pressed():
	_start_listening("skill3", skill3_button)

func _on_skill4_button_pressed():
	_start_listening("skill4", skill4_button)

func _on_skill5_button_pressed():
	_start_listening("skill5", skill5_button)

func _on_dash_button_pressed():
	_start_listening("dash", dash_button)

func _start_listening(action: String, button: Button):
	if is_listening:
		return
	
	current_listening_action = action
	current_listening_button = button
	is_listening = true
	
	button.text = "请按键..."
	button.modulate = Color.YELLOW
	
	# 设置输入处理
	set_process_input(true)

func _input(event):
	if not is_listening:
		return
	
	# 检查是否是有效的输入事件
	if _is_valid_binding_event(event):
		_bind_key(event)
		_stop_listening()
		get_viewport().set_input_as_handled()
	elif event is InputEventKey and event.pressed and event.keycode == KEY_ESCAPE:
		_stop_listening()
		get_viewport().set_input_as_handled()

func _is_valid_binding_event(event: InputEvent) -> bool:
	if event is InputEventKey and event.pressed:
		return true
	elif event is InputEventMouseButton and event.pressed:
		return true
	
	return false

func _bind_key(event: InputEvent):
	if not UserSettings or current_listening_action == "":
		return
	
	UserSettings.rebind_key(current_listening_action, event)

func _stop_listening():
	is_listening = false
	current_listening_action = ""
	current_listening_button = null
	set_process_input(false)
	
	# 恢复所有按钮状态
	_update_keybinding_buttons()
	_reset_button_colors()

func _reset_button_colors():
	# 重置所有按钮的颜色
	var buttons = [attack_button, interaction_button, w_button, s_button, a_button, d_button, 
				   skill1_button, skill2_button, skill3_button, skill4_button, skill5_button, dash_button]
	
	for button in buttons:
		if button:
			button.modulate = Color.WHITE

func _on_key_binding_changed(_action_name: String, _new_event: InputEvent):
	# 更新对应的按钮显示
	_update_keybinding_buttons()
